package service

import (
	"context"
	"go.uber.org/zap"
	"mall-portalv2/global"
	"mall-portalv2/internal/model"
)

func (s *Service) CreatePayLogs(ctx context.Context, insertData model.OmsPayLogs) (model.OmsPayLogs, error) {
	// TODO: 写入数据库
	result, err := s.dao.CreatePayLogs(ctx, insertData)
	if err != nil {
		return model.OmsPayLogs{}, err
	}

	//btPayLogs, err := json.Marshal(result)
	//if err == nil {
	//	global.Log.Error("序列化出了点问题", zap.Error(err))
	//	return result, err
	//}
	//
	//// TODO: 写入缓存
	//if err := s.RedisServiceSet(ctx, fmt.Sprintf("pay_data_%d", insertData.OrderId), string(btPayLogs), time.Minute*15); err != nil {
	//	global.Log.Error("redis存储出了点错误", zap.Error(err))
	//	return result, err
	//}
	return result, err
}

func (s *Service) QueryPayLogsWhereOrderId(ctx context.Context, OrderId int64, payType int, CloseStatus int) (model.OmsPayLogs, error) {

	var (
		result model.OmsPayLogs
		err    error
	)

	//RedisResult, err := s.dao.RedisDaoGet(ctx, fmt.Sprintf("pay_data_%d", OrderId))
	//if err != nil {
	//	if err == redis.Nil {
	//		global.Log.Info("查找数据库", zap.Any("data", OrderId))
	//		result, err = s.dao.QueryPayLogsWhereOrderId(ctx, OrderId, payType)
	//		if err != nil {
	//			return result, err
	//		}
	//		return result, nil
	//	} else {
	//		return result, err
	//	}
	//}
	//
	////result.SendData = string(RedisResult)
	//
	//global.Log.Info("查看结果", zap.Any("RedisResult", string(RedisResult)))
	//
	//if err = json.Unmarshal(RedisResult, &result); err != nil {
	//	return result, err
	//}

	global.Log.Info("查找数据库", zap.Any("data", OrderId))
	result, err = s.dao.QueryPayLogsWhereOrderIdAndStatus(ctx, OrderId, payType, CloseStatus)
	if err != nil {
		return result, err
	}
	return result, nil
}

func (s *Service) UpdatePayLogsWhereOrderId(ctx context.Context, OrderId int64, updates map[string]interface{}) error {
	if err := s.dao.UpdatePayLogsWhereOrderId(ctx, OrderId, updates); err != nil {
		return err
	}
	return nil
}

func (s *Service) QueryPayLogsWhereOrderIdNoWay(ctx context.Context, OrderId int64) ([]model.OmsPayLogs, error) {

	var (
		result []model.OmsPayLogs
		err    error
	)

	global.Log.Info("查找数据库", zap.Any("data", OrderId))
	result, err = s.dao.QueryPayLogsWhereOrderIdNoWay(ctx, OrderId)
	if err != nil {
		return result, err
	}
	return result, nil
}

func (s *Service) QueryPayLogsWherePaySnNoWay(ctx context.Context, PaySn string) (model.OmsPayLogs, error) {

	var (
		result model.OmsPayLogs
		err    error
	)

	global.Log.Info("查找数据库", zap.Any("data", PaySn))
	result, err = s.dao.QueryPayLogsWherePaySnNoWay(ctx, PaySn)
	if err != nil {
		return result, err
	}
	return result, nil
}

func (s *Service) QueryPayLogsWhereOrderIdAndPaySnNoWay(ctx context.Context, OrderId int64, PaySn string) (model.OmsPayLogs, error) {

	var (
		result model.OmsPayLogs
		err    error
	)

	global.Log.Info("查找数据库", zap.Any("data", OrderId))
	result, err = s.dao.QueryPayLogsWhereOrderIdAndPaySnNoWay(ctx, OrderId, PaySn)
	if err != nil {
		return result, err
	}
	return result, nil
}
